﻿using System;
using System.Windows.Forms;

namespace DisplaySC
{
    public partial class Main : Form
    {
        private Trend t;
        private const string URL = "http://sc.kkcai.net/sc{0}.html?{1}";

        public Main()
        {
            InitializeComponent();
        }

        private ConfigInfo GetCfg()
        {
            ConfigInfo cfg = new ConfigInfo();

            cfg.Type = rbtn1.Checked ? 1 : (rbtn2.Checked ? 2 : 3);
            
            cfg.Color = cbColor.SelectedIndex;
            cfg.Font = cbFont.SelectedIndex;
            cfg.IsBorder = chkBorder.Checked;
            cfg.IsOmit = chkOmit.Checked;

            cfg.Video = rbtnVideo1.Checked ? 1 : (rbtnVideo2.Checked ? 2 : 3);

            cfg.IsLine = chkRowLine.Checked;
            cfg.IsDS = chkDS.Checked;
            cfg.IsDX = chkDX.Checked;
            cfg.IsHZ = chkHZ.Checked;

            cfg.IsQ1 = chkQ1.Checked;
            cfg.IsQ2 = chkQ2.Checked;
            cfg.IsQ3 = chkQ3.Checked;

            cfg.IsGroup = chkGroup.Checked;
            cfg.IsCarNumber = cbCarNum.SelectedIndex == 0;
            
            return cfg;
        }

        #region 进入首页

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("http://www.kkcai.net");
            }
            catch { }
        }
        #endregion

        #region 通知栏

        private void goMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("http://www.kkcai.net");
            }
            catch { }
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定要退出电子走势图控制台吗？", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void hideMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void showMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;
                this.Hide();
            }
            else if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.Activate();
            }
        }
        #endregion

        private void Main_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        private void Main_Load(object sender, EventArgs e)
        {
            ReadConfig();
            
            if (new Manager().Valid())
            {
                return;
            }

            notifyIcon1.Visible = false;
            
            new Login().ShowDialog();
        }

        #region 启动、刷新、关闭

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (!notifyIcon1.Visible)
                notifyIcon1.Visible = true;

            btnExit.Enabled = true;
            btnRefresh.Enabled = true;
            btnStart.Enabled = false;

            ConfigInfo cfg = GetCfg();
            Manager.WriteConfig(cfg);

            try
            {
                if (t == null)
                    t = new Trend();

                t.Show();
                t.webBrowser1.Navigate(String.Format(URL, cbServer.SelectedIndex, cfg.ToString()));
            }
            catch
            {
                btnStart.Enabled = true;
                btnExit.Enabled = false;
                btnRefresh.Enabled = false;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (t != null)
            {
                t.Close();
                t = null;

                btnStart.Enabled = true;
                btnRefresh.Enabled = false;
                btnExit.Enabled = false;
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            ConfigInfo cfg = GetCfg();
            Manager.WriteConfig(cfg);

            if (t != null)
                t.webBrowser1.Navigate(String.Format(URL, cbServer.SelectedIndex, cfg.ToString()));
        }
        #endregion

        #region 读写配置文件

        private void ReadConfig()
        {
            try
            {
                ConfigInfo cfg = Manager.GetConfig();

                switch (cfg.Type)
                {
                    case 1: rbtn1.Checked = true; break;
                    case 2: rbtn2.Checked = true; break;
                    case 3: rbtn3.Checked = true; break;
                    default:
                        break;
                }

                switch (cfg.Video)
                {
                    case 1: rbtnVideo1.Checked = true; break;
                    case 2: rbtnVideo2.Checked = true; break;
                    case 3: rbtnVideo3.Checked = true; break;
                    default:
                        break;
                }

                cbColor.SelectedIndex = cfg.Color;
                cbFont.SelectedIndex = cfg.Font;
                cbServer.SelectedIndex = cfg.Server;
                cbCarNum.SelectedIndex = cfg.IsCarNumber ? 0 : 1;

                chkOmit.Checked = cfg.IsOmit;
                chkBorder.Checked = cfg.IsBorder;
                chkRowLine.Checked = cfg.IsLine;
                chkGroup.Checked = cfg.IsGroup;

                chkDS.Checked = cfg.IsDS;
                chkDX.Checked = cfg.IsDX;
                chkHZ.Checked = cfg.IsHZ;

                chkQ1.Checked = cfg.IsQ1;
                chkQ2.Checked = cfg.IsQ2;
                chkQ3.Checked = cfg.IsQ3;
            }
            catch
            {
                return;
            }
        }
        #endregion

        #region 旋转屏幕

        private void ChangeScreen(string direct)
        {
            Screen scr = Screen.AllScreens[0];
            if (Screen.AllScreens.Length > 1)
            {
                foreach (Screen item in Screen.AllScreens)
                {
                    if (!item.Primary)
                    {
                        scr = item;
                        //break;
                    }
                }
            }

            DEVMODE dm = DisplayHeler.CreateDevmode();

            DisplayHeler.EnumDisplaySettings(scr.DeviceName, -1, ref dm);

            int temp = dm.dmPelsHeight;
            dm.dmPelsHeight = dm.dmPelsWidth;
            dm.dmPelsWidth = temp;

            int index = dm.dmDisplayOrientation;

            if (direct == "left")
                dm.dmDisplayOrientation = (index == 3) ? 0 : index + 1;
            else
                dm.dmDisplayOrientation = (index == 0) ? 3 : index - 1;


            DisplayHeler.ChangeDisplaySettingsEx(scr.DeviceName, ref dm, null, 0, "");
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            ChangeScreen("left");
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            ChangeScreen("right");
        }
        #endregion

    }
}
